<template>
  <div class="shopping-cart">
    <h1>购物车</h1>
    <div class="cart-container" v-if="cartItems.length > 0">
      <div class="cart-items">
        <div class="cart-item" v-for="item in cartItems" :key="item.name">
          <div class="item-info">
            <h3>{{ item.name }}</h3>
            <p class="price">¥{{ item.price }}</p>
          </div>
          <div class="quantity-control">
            <button @click="decrement(item.name)" class="quantity-btn">-</button>
            <span class="quantity">{{ item.quantity }}</span>
            <button @click="increment(item.name)" class="quantity-btn">+</button>
          </div>
          <div class="item-total">
            <p>小计：¥{{ item.price * item.quantity }}</p>
          </div>
        </div>
      </div>
      <div class="cart-summary">
        <div class="total">总价：¥{{ totalPrice }}</div>
      </div>
    </div>
    <div class="empty-cart" v-else>
      <p>购物车是空的</p>
      <router-link to="/" class="continue-shopping">去购物</router-link>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'

export default {
  name: 'ShoppingCart',
  computed: {
    ...mapState({
      cart: state => state.cart
    }),
    ...mapGetters(['totalPrice']),
    cartItems() {
      return this.cart.filter(item => item.quantity > 0)
    }
  },
  methods: {
    ...mapMutations({
      increment: 'incrementQuantity',
      decrement: 'decrementQuantity'
    })
  }
}
</script>

<style scoped>
.shopping-cart {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
}

.cart-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.cart-items {
  margin-bottom: 20px;
}

.cart-item {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.cart-item:last-child {
  border-bottom: none;
}

.item-info h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 1.1em;
}

.price {
  color: #e74c3c;
  margin: 5px 0;
  font-weight: bold;
}

.quantity-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.quantity-btn {
  background-color: #ecf0f1;
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1.2em;
  display: flex;
  align-items: center;
  justify-content: center;
}

.quantity-btn:hover {
  background-color: #dcdde1;
}

.quantity {
  font-size: 1.1em;
  min-width: 30px;
  text-align: center;
}

.item-total {
  text-align: right;
  font-weight: bold;
  color: #2c3e50;
}

.cart-summary {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 2px solid #eee;
  text-align: right;
}

.total {
  font-size: 1.4em;
  font-weight: bold;
  color: #e74c3c;
}

.empty-cart {
  text-align: center;
  padding: 40px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.empty-cart p {
  color: #7f8c8d;
  margin-bottom: 20px;
  font-size: 1.2em;
}

.continue-shopping {
  display: inline-block;
  background-color: #2ecc71;
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  text-decoration: none;
  transition: background-color 0.3s;
}

.continue-shopping:hover {
  background-color: #27ae60;
}
</style> 